package com.ruoyi.paipan.common;

import java.text.SimpleDateFormat;
import java.util.*;

public class Zwinfo {
    private static final String str = null;
    private Cdnongli cdnongli;
    private LinkedHashMap<String,Object> cfg = new LinkedHashMap<>();
    private List<String> dizhi = Arrays.asList("亥", "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌");
    private List<String> dxshu = Arrays.asList(str, "一", "二", "三", "四", "五", "六", "七", "八", "九");
    private List<String> Dnmonth = Arrays.asList(str, "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "冬", "腊");
    private List<String> Dnday = Arrays.asList(str, "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十");
    private List<String> wx = Arrays.asList("木", "火", "土", "金", "水");
    public LinkedHashMap<String,Object> info = new LinkedHashMap<>();

    public Zwinfo(LinkedHashMap<String,Object> cfg) {
        this.cfg = cfg;
        this.cdnongli = new Cdnongli();
        this.set_info();
    }


    public void set_info()
    {
        try{
            String birthday = (String) this.cfg.get("date");
            int nian = 1970;
            int yue = 1;
            int ri = 1;
            int hh=0;
            int mm=0;
            long bjdate = 0 ;
            try{
                Calendar instance = Calendar.getInstance();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-M-dd H:m");
                Date parse = formatter.parse(birthday);
                instance.setTime(parse);
                bjdate = instance.getTimeInMillis() / 1000;
                nian = instance.get(Calendar.YEAR);
                yue = instance.get(Calendar.MONTH)+1;
                ri = instance.get(Calendar.DAY_OF_MONTH);
                hh = instance.get(Calendar.HOUR_OF_DAY);
                mm = instance.get(Calendar.MINUTE);
                SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");
                this.info.put("glstr",formatter1.format(instance.getTime()));
            }catch (Exception e){
                this.info.put("glstr","1970年01月01日00时00分");
            }

            List<String> narr = this.cdnongli.SolarToLunar(nian, yue, ri);
            int i1 = (int) ((Math.ceil((double) hh / 2) + 1) % 12);
            String ntime = this.dizhi.get(i1);
            ntime = hh == 23 ? "夜" + ntime : ntime;
            int zw_nmonth;
            if (Integer.parseInt(narr.get(7)) > 0 && Integer.parseInt(narr.get(4)) > Integer.parseInt(narr.get(7))) {
                zw_nmonth = Integer.parseInt(narr.get(4)) - 1;
                if (Integer.parseInt(narr.get(4)) == Integer.parseInt(narr.get(7)) + 1) {
                    if (Boolean.parseBoolean((String) this.cfg.get("runfen"))) {
                        zw_nmonth = Integer.parseInt(narr.get(4));
                    } else {
                        if (Integer.parseInt(narr.get(5)) > 15) zw_nmonth = Integer.parseInt(narr.get(4));
                    }
                }
            } else {
                zw_nmonth = Integer.parseInt(narr.get(4));
            }
            int nday = Integer.parseInt(narr.get(5));
            List<String> nlarr = Arrays.asList(narr.get(0), narr.get(1), narr.get(2), ntime);
            int s = Integer.parseInt(nlarr.get(0));
            int ytgx = (s - 1864 + 1) % 10;
            int ydzx = (s - 1864 + 1) % 12;
            List<String> sizhu = (List<String>)this.cfg.get("sizhu");
            this.info.put("nlarr", nlarr);
            this.info.put("nlstr",s + "年" + nlarr.get(1) + nlarr.get(2) + "日" + nlarr.get(3) + "时");
            this.info.put("ygz",sizhu.get(0) + sizhu.get(1));
            this.info.put("mgz",sizhu.get(4) + sizhu.get(5));
            this.info.put("dgz", sizhu.get(8)+ sizhu.get(9));
            this.info.put("hg", sizhu.get(12));
            this.info.put("hz", sizhu.get(13));
            this.info.put("hgx", sizhu.get(14));
            this.info.put("hzx", sizhu.get(15));
            this.info.put("hgz", (String)this.info.get("hg") + this.info.get("hz"));
            this.info.put("nayin", this.cfg.get("nayin"));
            LinkedHashMap<String,Object> cfg = this.cfg;
            int finalZw_nmonth = zw_nmonth;
            LinkedHashMap<String,Object> info = this.info;
            long finalBjdate = bjdate;
            LinkedHashMap<String,Object> config = new LinkedHashMap<String,Object>(){{
                    put("ismobile", cfg.get("ismobile"));
                    put("sex", cfg.get("sex"));
                    put("csmode", cfg.get("csmode"));
                    put("date", finalBjdate);
                    put("ytgx", ytgx);
                    put("ydzx", ydzx);
                    put("cm", finalZw_nmonth);
                    put("cd", nday);
                    put("hzx", info.get("hzx"));
            }};
            Cdziwei zw = new Cdziwei(config);
            Calendar instance1 = Calendar.getInstance();
            int i = instance1.get(Calendar.YEAR);
            this.info.put("sui", i - nian + 1);
            this.info.put("juname", zw.get_zwju_name());
            this.info.put("pan", zw.pai_zwpan());
            this.info.put("sihuaname", zw.get_sihua_name());
            this.info.put("minggong", zw.get_mgongz());
            LinkedHashMap<Integer, LinkedHashMap<String, String>> tfxing = zw.get_tfxing();
            LinkedHashMap<Integer, Object> tfxing1 = new LinkedHashMap<>();
            tfxing.forEach((k, v) -> {
                if(v.containsKey("id")){
                    tfxing1.put(k, v);
                }else{
                    String[] temp= new String[v.size()];
                    for (String s1 : v.keySet()) {
                        temp[Integer.parseInt(s1)] = v.get(s1);
                    }
                    tfxing1.put(k, temp);
                }

            });
            LinkedHashMap<Integer, LinkedHashMap<String, String>> zwxing = zw.get_zwxing();
            LinkedHashMap<Integer, Object> zwxing1 = new LinkedHashMap<>();
            zwxing.forEach((k, v) -> {
                if(v.containsKey("id")){
                    zwxing1.put(k, v);
                }else{
                    String[] temp= new String[v.size()];
                    for (String s1 : v.keySet()) {
                        temp[Integer.parseInt(s1)] = v.get(s1);
                    }
                    zwxing1.put(k, temp);
                }

            });
            this.info.put("zwxing", zwxing1);
            this.info.put("tfxing", tfxing1);
            this.info.put("mgindex", zw.mgindex);
            this.info.put("shengong", zw.get_sgongz());
            this.info.put("mingzhu", zw.get_mingzhu());
            this.info.put("shenzhu", zw.get_shenzhu());
            this.info.put("dou", zw.get_doujun());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public LinkedHashMap<String, Object> get_info()
    {
        return this.info;
    }
}
